/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.sys.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.rf.sys.utils.ConfigUtils;
import com.rf.sys.utils.JsonUtils;
import com.rf.sys.utils.PageUtils;
import com.rf.sys.utils.TreeObject;
import com.rf.sys.utils.TreeUtil;
import com.rf.sys.utils.UuidUtil;
import com.rf.sys.frm.BaseController;
import com.rf.sys.model.Resources;
import com.rf.sys.model.RoleRes;
import com.rf.sys.service.ResourcesService;
import com.rf.sys.service.RoleResService;

@Controller
@RequestMapping("/sys/resources")
public class ResourcesController extends BaseController{
	/**
	 * 日志
	 */
	private static Logger log = LoggerFactory.getLogger(ResourcesController.class);
	private static final long serialVersionUID = -120160623160132163L;
	
	@Resource
	private ResourcesService resourcesService;
	@Resource
	private RoleResService roleResService;
	
	/**
	 * 初始化页面信息
	 * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
	 */
	@RequestMapping(value = "index")
    public ModelAndView index (HttpServletRequest request, HttpServletResponse response) throws Exception{
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("/sys/resources/resources_list");
		return modelAndView; 
    }
    
    /**
	 * 页面左侧树信息
	 * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "query")
    @ResponseBody
    public void query (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	//获取所有菜单
    	List<Resources> rList = resourcesService.getResources();
    	//根据ztree结构转换数据结构
    	List<TreeObject> tList = new ArrayList<TreeObject>();
    	//虚拟根节点
    	TreeObject to = new TreeObject();
    	to.setId("0");
		to.setName("菜单列表");
		to.setLevel("0");
		to.setOpen(true);
		tList.add(to);
		//循环插入节点数据
		for (Resources r : rList) {
			TreeObject ts = new TreeObject();
			ts.setId(r.getId());
			ts.setName(r.getName());
			ts.setParentId(r.getParentId());
			ts.setLevel(r.getResLevel());
			tList.add(ts);
		}
    	super.responsePrint(request, response, JsonUtils.listToJson(tList));
    }
    
    /**
	 * 页面树信息--用于角色分配菜单
	 * @param request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "queryResources")
    @ResponseBody
    public void queryResources (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String rid = request.getParameter("rid");
    	List<Resources> rList = resourcesService.getResources();//获取所有菜单
    	RoleRes roleRes = new RoleRes();
    	roleRes.setRoleId(rid);
    	List<RoleRes> rrList = roleResService.getRoleRes(roleRes);//获取该角色下的所有菜单
    	//根据ztree结构转换数据结构
    	List<TreeObject> tList = new ArrayList<TreeObject>();
    	//虚拟根节点
    	TreeObject to = new TreeObject();
		to.setId("0");
		to.setName("菜单列表");
		to.setLevel("0");
		to.setOpen(true);
		tList.add(to);
		//循环插入节点数据
		for (Resources r : rList) {
			TreeObject ts = new TreeObject();
			ts.setId(r.getId());
			ts.setName(r.getName());
			ts.setParentId(r.getParentId());
			ts.setLevel(r.getResLevel());
			//判断当前角色是否分配了改菜单，如果分配了，则被勾选
			for(RoleRes rr : rrList){
				if(rr.getResId().equals(r.getId())){
					ts.setChecked(true);
					break;
				}
			}
			tList.add(ts);
		}
    	super.responsePrint(request, response, JsonUtils.listToJson(tList));
    }
    
    /**
     * 编辑页面初始化信息
     * @param request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "edit")
    @ResponseBody
    public void edit (HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	JSONObject returnObj = new JSONObject();
		String id = request.getParameter("id");
		if(id != null && !id.equals("")){
			Resources resources = resourcesService.getResourcesByID(id);
			returnObj.element("resources", resources);
		}
		super.responseJson(request, response, returnObj.toString());
    }
    
    /**
     * 保存数据信息
     * @param Resources,request,response
	 * @return ModelAndView
	 * @throws ServletException,IOException
     */
    @RequestMapping(value = "save")
    public void save (Resources resources, HttpServletRequest request, HttpServletResponse response ) throws Exception{
    	String ret = "success";
		if(resources !=null && resources.getId()!=null && !resources.getId().equals("")){
			//修改
			resourcesService.updateResources(resources);
		}else{
			//新增
			resourcesService.addResources(resources);
		}
		super.resPrint(request, response, ret);//成功
    }
    
    /**
     * 删除数据信息
     * @param Resources,request,response
	 * @return JSON
	 * @throws ServletException,IOException
	 */
    @RequestMapping(value = "delete")
    @ResponseBody
    public void delete (Resources resources, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	JSONObject returnObj = new JSONObject();
    	resources.setState(ConfigUtils.DELSTATE);
    	resourcesService.updateResources(resources);
		returnObj.element("message", "success");//成功
		super.responseJson(request, response, returnObj.toString());
    }
}


